﻿/*
* http://www.cnblogs.com/fox23/archive/2008/12/24/windows-mobile-ril-in-compact-framework.html
*/

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;

namespace CellIDSample
{
    public partial class Form1 : Form
    {
        StreamWriter sw;
        static int count = 0;
        int time;
        const int DEFAULT_TIME = 4000;
        Timer cellInfoTimer;
        public Form1()
        {
            InitializeComponent();
            initializeTimer();
        }

        private void initializeTimer()
        {
            cellInfoTimer = new Timer();
            cellInfoTimer.Tick += new EventHandler(cellInfoOnTimedEvent);
            cellInfoTimer.Interval = DEFAULT_TIME;
            /*
            configTimer.Interval = 10000;
            configTimer.Tick += new EventHandler(configOnTimedEvent);
            configTimer.Enabled = true;
            cellInfoTimer.Tick += new EventHandler(cellInfoOnTimedEvent);
            //*/
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            cellInfoTimer.Enabled = true;

            /*此段代码会在My Device根目录下创建CellIDs.txt的文本文件并将信息写入
           sw = new StreamWriter(@"\CellIDs.txt", true, Encoding.ASCII);
           sw.WriteLine("\r\nBSid:"+RILHelper.rilCellTowerInfo.dwBaseStationID.ToString()+"\nAreaID:"+RILHelper.rilCellTowerInfo.dwLocationAreaCode.ToString());
           sw.WriteLine(DateTime.Now.ToString());
            sw.Flush();
           sw.Close();
            //*/
        }

        /************************************************************************/
        /* CellInfo计时器每次到点触发的方法，即记录一次CellID信息               */
        /************************************************************************/
        private void cellInfoOnTimedEvent(object myObject, EventArgs myEventArgs)
        {
            DateTime today = DateTime.Now;
            String cellInfo = RILHelper.GetCellTowerInfo() + "|" + today.Year + "/" + today.Month + "/" + today.Day + "|" + (today.Hour - 1) + ":" + today.Minute + ":" + today.Second;
            //textBox1.Text = cellInfo;
            updateRegistryKey(cellInfo);
            updateCellInfoTime();
        }

        private void updateCellInfoTime()
        {
            RegistryKey rk = Registry.CurrentUser.CreateSubKey("CellInfo\\Test");
            string configInfo = rk.GetValue("ConfigInfo") + "";
            if (configInfo.Equals(""))
            {
                rk.SetValue("ConfigInfo", DEFAULT_TIME + "", RegistryValueKind.String);
                configInfo = DEFAULT_TIME + "";
            }
            try
            {
                //time = Int32.Parse(configInfo);
                Random r = new Random();
                int temp = r.Next(1, 10);
                textBox1.Text += "<" + temp + ">";
                time = 1000 * temp;
            }
            catch (System.FormatException ex)
            {
                time = DEFAULT_TIME;
            }
            cellInfoTimer.Interval = time;     
        }

        /************************************************************************/
        /* 将获取的CellID等信息更新到手机注册表中，如果注册表被写满，则将最老的数据覆盖 */
        /************************************************************************/
        private void updateRegistryKey(string cellInfo)
        {
            ++ count;
            RegistryKey rk = Registry.CurrentUser.CreateSubKey("CellInfo\\Test");
            string originData = rk.GetValue("Record") + "";
            if (!originData.Equals(""))
                originData += "&";
            
            try
            {
            	rk.SetValue("Record" , originData + "[" + count + "]" + cellInfo , RegistryValueKind.String);
            }
            catch (System.IO.IOException ex)//已经写满，需要删除一些旧记录
            {
                int index = originData.IndexOf('&') + 1;
                originData = originData.Substring(index);
                index = originData.IndexOf('&') + 1;
                originData = originData.Substring(index);
                rk.SetValue("Record", originData + "[" + count + "]" + cellInfo, RegistryValueKind.String);
            }
            textBox1.Text = rk.GetValue("Record") + "";
        }
    }
}